const express=require('express');
const app=express();
const ejs =require('ejs');
const bodyParser=require('body-parser');
var cookieParser = require('cookie-parser')
var session = require('express-session')
app.use(cookieParser())

app.use(bodyParser.urlencoded({ extended: false }))
 
// parse application/json
app.use(bodyParser.json())
app.set('trust proxy', 1) // trust first proxy
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}))
app.engine('html',ejs.__express);
app.set('view engine','html');
// app.set("view engine","ejs");
app.use(express.static("static"));//内置中间件
//应用级中间件
app.use((req,res,next)=>{
  console.log(new Date());
  next();
});
app.get('/',(req,res)=>{
  let title='我是title';
  res.cookie("username",'zhangsan1',{maxAge:10000})
  req.session.username='wjl';
  res.render("index",{
   title:title
  });
  
})
app.get('/login',(req,res,next)=>{
  let username=req.cookies.username;
  let u=req.session.username;
  console.log(u);
  res.render("login",{
    username:username
  });
  
})
app.post('/dologin',(req,res,next)=>{
  let body=req.body;
  console.log(body.username);
  
  res.send('提交成功');
})

app.get('/news',(req,res)=>{
    let person={
   name:'zhangsan',
   age:11
    }
    let address='<h3>hangzhou</h3>';
    res.render('news',{
      person:person,address:address,flag:false
    });
})
// app.get('/news/add',(req,res,next)=>{
//    res.send('this is news add');
//    next();
// });
// app.get('/news/:id',(req,res,next)=>{
//    console.log('this is zjj');
// });
//错误处理中间件
app.use((req,res,next)=>{
  res.status(404).send('404')
})
app.listen(8888);